# 二手奢侈品估价模型

# 导入 matplotlib 的 pyplot 子库，提供绘图API
import matplotlib.pyplot as plt
# 导入 sklearn 的 linear_model 模块
from sklearn import linear_model
# 导入 科学计算库
import numpy as np

# 设置X轴标签
plt.xlabel('市场价格')
# 设置Y轴标签
plt.ylabel('回收价格')

scatter_alpha = 1

# S级
# datas = np.transpose(np.loadtxt('s.txt', skiprows=1))
# price1s = datas[0, :]  # 市场价
# price2s = datas[3, :]  # 销售价
# plt.scatter(price1s, price2s, c='c', marker='o', s=10, alpha=scatter_alpha) # 绘制散点图
# datasets_X = price1s
# datasets_Y = price2s

# A级
# dataa = np.transpose(np.loadtxt('a.txt', skiprows=1))
# price1a = dataa[0, :]  # 市场价
# price2a = dataa[3, :]  # 销售价
# # plt.scatter(price1a, price2a, c='r', marker='o', s=10, alpha=scatter_alpha)
# plt.scatter(price1a, price2a, c='c', marker='o', s=10, alpha=scatter_alpha)
# datasets_X = price1a
# datasets_Y = price2a

# B级
# datab = np.transpose(np.loadtxt('b.txt', skiprows=1))
# price1b = datab[0, :]  # 市场价
# price2b = datab[3, :]  # 销售价
# plt.scatter(price1b, price2b, c='c', marker='o', s=10, alpha=scatter_alpha)
# # plt.scatter(price1b, price2b, c='r', marker='o', s=10, alpha=scatter_alpha)
# datasets_X = price1b
# datasets_Y = price2b

# C级
datac = np.transpose(np.loadtxt('c.txt', skiprows=1))
price1c = datac[0, :]  # 市场价
price2c = datac[3, :]  # 销售价
plt.scatter(price1c, price2c, c='c', marker='o', s=10, alpha=scatter_alpha)
# plt.scatter(price1c, price2c, c='k', marker='o', s=10, alpha=scatter_alpha)
datasets_X = price1c
datasets_Y = price2c

# plt.legend(['99成新','9成新','8成新','8成新以下'])

# 数据长度
length = len(datasets_X)
# 对 datasets_X 转化为数组，并转置（变成二维），以符合线性回归拟合函数输入参数要求
datasets_X = np.array(datasets_X).reshape([length, 1])
# 将 datasets_Y 转化为数组
datasets_Y = np.array(datasets_Y)
minX = min(datasets_X)
maxX = max(datasets_X)
# 以 datasets_X 的最大值和最小值建立等差数列，方便后续画图
X = np.arange(minX, maxX).reshape([-1, 1])
# 建立线性回归方程，拟合数据
linear = linear_model.LinearRegression()
linear.fit(datasets_X, datasets_Y)
# 查看回归方程系数
print('系数因子 coef ficient：', linear.coef_)
# 查看回归方程截距
print('截距 intercept：', linear.intercept_)
# plot 绘制直线
plt.plot(X, linear.predict(X), color='black')
# 设置标题
plt.title('8成新以下')
plt.show()
